<?php
declare(strict_types=1);

namespace Modules\WeChat\Service;

use HiCommon\Repository\GoodsSpecsRepository;
use HiCommon\Repository\OrderMallRepository;
use HiCommon\Repository\OrderRepairstoreRepository;

class GoodsOrderService extends BaseService
{
    /**
     *获取商品订单列表
     */
    public function getGoodsOrderList($n_shop_id, $arr_input)
    {
        if (empty($n_shop_id)) {
            return hello_error('请登录');
        }
        $arr_option['where'] = ['shop_id' => $n_shop_id];//商户id
        $arr_option['with'] = ['getGoodsInfo', 'getOrderMallDetail', 'getUserInfo'];
        $arr_list = OrderMallRepository::get_list((array)$arr_option, (array)$arr_input);

        $arr_list = list_page_data($arr_list);

        if ($arr_list['data']) {
            $result = array();
            foreach ($arr_list['data'] as $key => $item) {
                $result[] = array(
                    "id" => $item['id'],
                    "order_no" => $item['order_no'],
                    "order_type" => $item['order_type'],
                    "status" => $item['status'],
                    "pay_status" => $item['pay_status'],
                    "delivery_status" => $item['delivery_status'],
                    "order_integral" => $item['order_integral'],
                    "order_amount" => $item['order_amount'],
                    "pay_time" => $item['pay_time'],
                    "created_at" => $item['created_at'],
                    "goods_id" => $item['goods_id'],
                    "goods_num" => $item['get_order_mall_detail']['goods_num'],
                    "goods_name" => $item['get_goods_info']['name'],
                    "goods_pic" => $item['get_goods_info']['pic_url']['full_src'],
                    "user_nickname" => $item['get_user_info']['nickname'],
                    "user_realname" => $item['get_user_info']['realname'],
                    "user_phone" => $item['get_user_info']['phone'],
                );
            }

            return hello_success('获取成功', ['list' => $result, 'page' => $arr_list['page_data']]);
        }

        return hello_error('暂无数据');
    }

    /**
     *积分商城订单详情
     */
    public function getGoodsOrderDetail($n_shop_id, $arr_input)
    {
        if (empty($n_shop_id)) {
            return hello_error('请登录');
        }
        $arr_option['with'] = ['getGoodsInfo', 'getUserInfo', 'getAddressInfo', 'getExpressInfo', 'getOrderMallDetail' => function ($query) {
            $query->with(['product']);
        }];
        $info = OrderMallRepository::get_one((int)$arr_input['order_id'], (array)$arr_option)->toArray();

        $arr_option['field'] = ['id', 'name'];
        $specs = GoodsSpecsRepository::get_one_by_where((array)['id' => $info['get_order_mall_detail']['goods_specs_id']]);
        $info['specs'] = $specs['name'];

        $result = array(
            "id" => $info['id'],
            "order_no" => $info['order_no'],
            "order_integral" => $info['order_integral'],
            "status" => $info['status'],
            "pay_status" => $info['pay_status'],
            "delivery_status" => $info['delivery_status'],
            "delivery_name" => $info['get_express_info']['name'],
            "delivery_no" => $info['delivery_no'],
            "delivery_price" => $info['delivery_price'],
            "delivery_time" => $info['delivery_time'],
            "pay_time" => $info['pay_time'],
            "created_at" => $info['created_at'],
            "goods_id" => $info['goods_id'],
            "goods_name" => $info['get_goods_info']['name'],
            "goods_num" => $info['get_order_mall_detail']['goods_num'],
            "goods_specs" => $info['specs'],
            "goods_pic" => $info['get_order_mall_detail']['product']['pic']['full_src'],
            "user_name" => $info['get_address_info']['username'],
            "user_phone" => $info['get_address_info']['phone'],
            "user_province" => $info['get_address_info']['province'],
            "user_city" => $info['get_address_info']['city'],
            "user_area" => $info['get_address_info']['area'],
            "user_address" => $info['get_address_info']['address'],
        );

        return hello_success('获取成功', $result);
    }

    /**
     *门店获取服务商品订单列表
     */
    public function getRepairOrderDist($n_shop_id, $arr_input)
    {
        if (empty($n_shop_id)) {
            return hello_error('请登录');
        }
        $arr_option['where'] = ['shop_id' => $n_shop_id];//商户id
        $arr_option['with'] = ['user', 'gtype', 'order_apprise', 'goods' => function ($query) {
            $query->with(['pic']);
        }];
        $data = OrderRepairstoreRepository::api_get_list($arr_option, (array)$arr_input);

        $arr_list = list_page_data($data);
        if ($arr_list['data']) {
            $result = array();
            foreach ($arr_list['data'] as $key => $item) {
                $result[] = array(
                    "id" => $item['id'],
                    "order_number" => $item['order_number'],
                    "order_price" => $item['order_price'],
                    "status" => $item['status'],
                    "pay_status" => $item['pay_status'],
                    "pay_time" => $item['pay_time'],
                    "created_at" => $item['created_at'],
                    "goods_type" => $item['gtype']['title'],
                    "goods_id" => $item['repairstore_goods_id'],
                    "goods_name" => $item['goods_name'],
                    "goods_pic" => $item['goods']['pic'][0]['picurl']['full_src'],
                    "order_apprise" => $item['order_apprise']['score'],
                    "user_nickname" => $item['user']['nickname'],
                    "user_realname" => $item['user']['realname'],
                    "user_phone" => $item['user']['phone'],
                    "order_status" => self::oaderStatus($item['status'], $item['pay_status'])
                );
            }

            return hello_success('获取成功', ['list' => $result, 'page' => $arr_list['page_data']]);
        }

        return hello_error('暂无数据');
    }

    //服务商品订单状态
    public function oaderStatus($status, $pay_status)
    {
        $state = '';
        if ($pay_status == 0) {
            $state = '待支付';
        } elseif ($pay_status == 1 && $status == 0) {
            $state = '待检修';
        } elseif ($pay_status == 1 && $status == 1) {
            $state = '待评价';
        } elseif ($pay_status == 2) {
            $state = '待退款';
        } elseif ($pay_status == 3) {
            $state = '已退款';
        }

        if ($status == 2) {
            $state = '已取消';
        } elseif ($status == 3) {
            $state = '已评价';
        } elseif ($status == 4) {
            $state = '已完成';
        }

        return $state;
    }
}